package component;

import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindow;
import entity.MarkDownData;
import markdown.MDFreeMarkProcessor;
import markdown.Processor;
import persistent.DataCenter;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class NoteListWindow {
    private JPanel jcontent;
    private JTextField topicEtf;
    private JButton createBtn;
    private JButton clearBtn;
    private JButton closeBtn;
    private JTable contentTable;


    public void init(){
        contentTable.setModel(DataCenter.TABLE_MODEL);
        contentTable.setEnabled(true);
    }


    public NoteListWindow(Project project, ToolWindow toolWindow) {
        this.init();
        createBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                VirtualFile virtualFile = FileChooser.chooseFile(FileChooserDescriptorFactory.createSingleFolderDescriptor(), project, project.getBaseDir());
                if(virtualFile!=null) {
                    String path = virtualFile.getPath();
                    String topic = topicEtf.getText();
                    String filePath = path + "/" + topic + ".md";
                    Processor processor = new MDFreeMarkProcessor();
                    try {
                        processor.process(new MarkDownData(topic, filePath, DataCenter.NOTE_LIST));
                        MessageDialogBuilder.yesNo("操作结果","生成成功!").show();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }

                }



            }
        });
        clearBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DataCenter.reset();

            }
        });
        closeBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                toolWindow.hide(null);
            }
        });
    }

    public JPanel getJcontent() {
        return jcontent;
    }
}
